﻿using System;
using System.Windows.Input;
using System.Windows.Threading;
using Core.Interfaces;
using Core.Settings;
using DataXml;
using TallyJ.Announcer;
using Core;

namespace TallyJ.Windows
{
  /// <summary>
  /// Interaction logic for Launch.xaml
  /// </summary>
  public partial class Launch
  {
    ///<summary></summary>
    public static RoutedCommand LaunchAnnouncerCommand = new RoutedCommand();

    ///<summary></summary>
    public static RoutedCommand LaunchDbToolsCommand = new RoutedCommand();

    ///<summary></summary>
    public static RoutedCommand LaunchFrontDeskCommand = new RoutedCommand();

    private IEnvironment _environment;


    public Launch()
    {
      InitializeComponent();

      _environment = new CurrentEnvironment(new CommunityImporter());

      _environment.Settings.FilePathChanged += OnDataChangedExternally;
      _environment.CommunityFileChanged += OnDataChangedExternally;


      CommandBindings.Add(new CommandBinding(LaunchDbToolsCommand, LaunchDbTools, CanLaunchDbTools));
      CommandBindings.Add(new CommandBinding(LaunchAnnouncerCommand, LaunchAnnouncer, CanLaunchAnnouncer));

      btnLaunchDbTools.Command = LaunchDbToolsCommand;
      btnLaunchAnnouncer.Command = LaunchAnnouncerCommand;

      ShowInfo();

      
      KeyDown += OnKeyDown;
    }

    private void ShowInfo()
    {
      try
      {

        var x = new AnnouncerPresenter(new AnnouncerWindow(), _environment);

        CommunityName.Content = _environment.Community.Name;
        File.Content = _environment.Settings.SourceXmlCommunityFilePath;
        CommunityCount.Content = x.PersonList.Count;
      }
      catch
      {
      }
    }

    private void OnDataChangedExternally(object sender, EventArgs e)
    {
      _environment.Community = null;
      Dispatcher.Invoke(DispatcherPriority.Normal, new Action(ShowInfo));
    }

    private void OnDataChangedExternally(object sender, DataChangedEvent e)
    {
      _environment.Community = null;
      Dispatcher.Invoke(DispatcherPriority.Normal, new Action(ShowInfo));
    }

    private void OnKeyDown(object sender, KeyEventArgs args)
    {
      switch (args.Key)
      {
        case Key.Escape:
          Close();
          break;
        case Key.A:
          LaunchAnnouncer(this, null);
          break;
        case Key.D:
          LaunchDbTools(this, null);
          break;
        case Key.F:
          break;
      }
    }

    private void CanLaunchAnnouncer(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = _environment.Settings.SourceXmlCommunityFilePath.HasContent();
    }

    private void LaunchAnnouncer(object sender, ExecutedRoutedEventArgs e)
    {
      var x = new AnnouncerPresenter(new AnnouncerWindow(), _environment);
      x.ShowDialog();
    }

    private void CanLaunchFrontDesk(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

    private void LaunchDbTools(object sender, ExecutedRoutedEventArgs e)
    {
      var w = new DbTools(_environment) {Owner = this};
      w.ShowDialog();
    }

    private void CanLaunchDbTools(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

    private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {

    }
  }
}